package ar.edu.uba.fi.jmit.graphvisualizer.client;

import java.io.Serializable;
import java.util.Map;

import ar.edu.uba.fi.jmit.graphvisualizer.client.widget.DynamicMenuStackPanel;
import ar.edu.uba.fi.jmit.graphvisualizer.client.widget.GraphVisualizerChart;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Application implements EntryPoint {
	
	private DynamicMenuStackPanel menu;
	private GraphVisualizerChart chart;
	private SecondaryInformationPanel secondaryInformationPanel;
	private DynamicMenuServiceAsync service = GWT.create(DynamicMenuService.class);
	private static boolean timerInitialized = false;
	private AlertListPanel alertPanel;
	
	private void loadMenu() {
		GWT.log("Loading Menu");
		RootPanel menuRootPanel = RootPanel.get("menu");
		if (menu == null) {
			menu = new DynamicMenuStackPanel(this);
			menu.setWidth("200px");
		}
		menuRootPanel.add(menu);
	}
	
	private void loadSecondaryInformationPanel() {
		GWT.log("Loading SecondaryInformationPanel");
		RootPanel secInfoPanel = RootPanel.get("secondaryInformation");
		if (secondaryInformationPanel == null) {
			secondaryInformationPanel = new SecondaryInformationPanel();
			secondaryInformationPanel.setWidth("200px");
			secondaryInformationPanel.setVisible(false);
		}
		secInfoPanel.add(secondaryInformationPanel);
	}	

	public void onModuleLoad() {
		GWT.log("On the onModuleLoad() of Application");
		this.loadMenu();
		this.loadSecondaryInformationPanel();
		this.loadAlertPanel();
		startTimer();
    }

	private void loadAlertPanel() {
		alertPanel = new AlertListPanel();
		alertPanel.setVisible(false);
		RootPanel alertsPanelContainer = RootPanel.get("alertsContainer");
		alertsPanelContainer.add(alertPanel);
	}

	/*
	 * TODO push, no pull! 
	 */
	private void startTimer() {
		if (!timerInitialized){
			Timer timer = new Timer() {
				public void run() {
					refreshDynamicMenu();
					refreshActiveChart();
					refreshAlertsPanel();
					refreshSecondaryInformation();
				}
			};
			timer.scheduleRepeating(2000);
			timerInitialized = true;
		}
	}
	
	private void refreshActiveChart() {
		if (chart != null)
			chart.update();
	}
	
	private void refreshDynamicMenu() {
		if (menu != null)
			menu.update();
	}
	
	private void refreshAlertsPanel() {
		alertPanel.update();
		
	}
	
	private void refreshSecondaryInformation() {
		if (secondaryInformationPanel != null && chart != null)
			secondaryInformationPanel.update(chart.getApplicationName(), chart.getMonitorName());
	}
	
	public void changeCurrentChart(String appName, String monitorName) {
		service.getData(appName, monitorName, new AsyncCallback<Map<String,Serializable>>() {

			@Override
			public void onSuccess(Map<String, Serializable> data) {
				RootPanel mainPanelRootPanel = RootPanel.get("mainContainer");
				if(chart != null) {
					mainPanelRootPanel.clear();
				}
				chart = ChartFactory.getChartForAppNameMonitorName(data);
				mainPanelRootPanel.add(chart);
				refreshActiveChart();
				refreshSecondaryInformation();
				refreshAlertsPanel();
			}
			
			@Override
			public void onFailure(Throwable e) {
				GWT.log("Application.changeCurrentChart: Failure", e);
			}			
		});		
	}
}

